package com.eigaland.change_machine.customview

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.View
import android.view.ViewGroup
import com.eigland.eigaland_lib.model.SeatPlan
import com.google.gson.Gson
import com.orhanobut.logger.Logger
import movie6.posprintercept2.extensions.dip


public class SeatViewFake(context: Context?, attrs: AttributeSet?) : View(context,attrs) {

    var letterArray = arrayOf("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z")


    var seatSelectedColor = ""

    var seatAvailableColor = ""

    var paint = Paint()

    var seatPlan : SeatPlan? = null

    var seatUnavailableColor = ""

    var maxWidth = 0f

    var maxHeight = 0f

    var seatHorizontalSize = 10

    var seatVSize = 10

    var startX = 0

    var startY = 0

    lateinit var setPlan:SeatPlan

    fun setSeatPlanData(seatPlan: SeatPlan){
        this.seatPlan = seatPlan
        invalidate()
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        //draw horizontal lable text
        //
        paint.color = Color.WHITE;
        paint.style = Paint.Style.FILL;
        paint.strokeWidth = 2f;
        paint.textSize = 30f;


        seatPlan?.let {
            maxWidth = dip(seatPlan?.w!!.toFloat())!!.toFloat()
            maxHeight = dip(seatPlan?.h!!.toFloat())!!.toFloat()

            startX = ((width - maxWidth) / 2).toInt()

            Logger.e("startX-->" + startX)

            drawLeftListAndRightList(canvas)
            drawSeat(canvas,true)
        }



    }

    fun drawLeftListAndRightList(canvas: Canvas?){

        seatPlan?.sections?.mapIndexed{index , secction ->
            //draw left indicator text
            canvas?.drawText(letterArray[index],startX.toFloat(),100f * index,paint)
            canvas?.drawText(letterArray[index],maxWidth - 10f,100f * index,paint)
        }

    }


    fun drawSeat(canvas: Canvas?,isCheck:Boolean){
        Logger.e("refresh")

        paint.strokeWidth = 3f
        paint.style = Paint.Style.STROKE
        paint.color = Color.WHITE
            seatPlan?.sections?.let {sections->
                sections.mapIndexed { index, section ->
                    section?.seats?.forEachIndexed{seatIndex ,seat ->
                    }
                }
            }


    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        val minimumWidth = maxWidth.toInt()
        val minimumHeight = maxHeight.toInt()

        val width = measureSize(minimumWidth, widthMeasureSpec)
        val height = measureSize(minimumHeight, heightMeasureSpec)
        setMeasuredDimension(width, height)

    }

    private fun measureSize(defaultSize: Int, spec: Int): Int {
        val specMode = View.MeasureSpec.getMode(spec)
        val specSize = View.MeasureSpec.getSize(spec)
        if (specMode == View.MeasureSpec.EXACTLY) {
            return specSize
        } else if (specMode == View.MeasureSpec.AT_MOST) {
            return Math.min(defaultSize, specSize)
        }
        return defaultSize
    }
}